package com.kc.web.controller.business;

import com.kc.business.domain.BizScenicBusiness;
import com.kc.business.service.IBizScenicBusinessService;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/9/9
 */
@RestController
@RequestMapping("/business/scenicBusiness")
public class BizScenicBusinessController extends BaseController {

    @Autowired
    IBizScenicBusinessService bizScenicBusinessService;

    /**
     * 获取商家列表
     */
    @PreAuthorize("@ss.hasPermi('business:scenicBusiness:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizScenicBusiness bizScenicBusiness) {
        startPage();
        List<BizScenicBusiness> list = bizScenicBusinessService.getList(bizScenicBusiness);
        for (BizScenicBusiness business : list) {
            business.setImages(new String[0]);
            //景区图片
            if (StringUtils.isNotBlank(business.getImage())) {
                String[] scenicImgs = StringUtils.split(business.getImage(), ",");
                business.setImages(scenicImgs);
            }
//            Integer[] typeArr = new Integer[2];
//            typeArr[0] = business.getMainTypeId();
//            typeArr[1] = business.getTypeId();
//            business.setTypeArr(typeArr);

        }
        return getDataTable(list);
    }

    /**
     * 编辑商家信息
     */
    @PreAuthorize("@ss.hasPermi('business:scenicBusiness:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BizScenicBusiness bizScenicBusiness) {
        //图片数组转字符串逗号分隔
        bizScenicBusiness.setImage(StringUtils.join(bizScenicBusiness.getImages(), ","));
//        bizScenicBusiness.setMainTypeId(bizScenicBusiness.getTypeArr()[0]);
//        bizScenicBusiness.setTypeId(bizScenicBusiness.getTypeArr()[1]);
        return toAjax(bizScenicBusinessService.edit(bizScenicBusiness));
    }
    /**
     * 获取商家详情
     */
    @PreAuthorize("@ss.hasPermi('business:scenicBusiness:info')")
    @GetMapping("/info/{id}")
    public AjaxResult getInfo(@PathVariable Integer id) {
        BizScenicBusiness bizScenicBusiness = bizScenicBusinessService.getBusinessInfoById(id);
        return AjaxResult.success(bizScenicBusiness);
    }

}
